<?php

class saveEmploymentContractAction extends baseTLUPimAction
{

    public function setForm(sfForm $form)
    {
        if (is_null($this->form)) {
            $this->form = $form;
        }
    }

    public function execute($request)
    {
        $empNumber = $request->getParameter('empNumber');
        $this->empNumber = $empNumber;

        $employmentContractId = $request->getParameter('employmentContractId');

        if (!$this->IsActionAccessible($empNumber)) {
            $this->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action'));
        }

        $this->employmentContractPermissions = $this->getDataGroupPermissions('employment-contract', $this->empNumber);

        $params = array('empNumber' => $empNumber, 'employmentContractId' => $employmentContractId, 'employmentContractPermissions' => $this->employmentContractPermissions);

        $this->setForm(new EmploymentContractForm(array(), $params));

        if ($request->isMethod('post')) {
            $this->form->bind($request->getParameter($this->form->getName()));

            if ($this->form->isValid()) {
                $result = $this->form->save();
                $this->getUser()->setFlash($result['messageType'], $result['message']);
                $this->redirect('tlupim/viewEmploymentContractList?empNumber=' . $empNumber);
            } else {
                $validationMsg = '';
                foreach ($this->form->getWidgetSchema()->getPositions() as $widgetName) {
                    if ($this->form[$widgetName]->hasError()) {
                        $validationMsg .= $this->form[$widgetName]->getError()->getMessageFormat();
                    }
                }
                $this->getUser()->setFlash('employmentcontract.warning', $validationMsg);
            }
        }
    }

}

